package org.example;
class Node<T> {
    T data;
    Node<T> next;

    public Node(T data) {
        this.data = data;
        this.next = null;
    }
}
// 定义链栈类
class LinkStackClass<T> {
    private Node<T> top;

    public LinkStackClass() {
        top = null;
    }

    // 入栈操作
    public void push(T item) {
        Node<T> newNode = new Node<>(item);
        newNode.next = top;
        top = newNode;
    }

    // 出栈操作
    public T pop() {
        if (empty()) {
            throw new RuntimeException("栈为空");
        }
        T item = top.data;
        top = top.next;
        return item;
    }

    // 判断栈是否为空
    public boolean empty() {
        return top == null;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        Node<T> current = top;
        while (current != null) {
            sb.append(current.data).append(" ");
            current = current.next;
        }
        return sb.toString();
    }
}